﻿using System;
using System.Web.UI.WebControls;

namespace TicketingCompany
{
    public partial class Events : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void UpdateItem(object sender, CommandEventArgs e)
        {
            int id = (int)e.CommandArgument;
        }

        protected void ButtonOpenCreateEventPanel_Click(object sender, EventArgs e)
        {
            PanelCreateEvent.Visible = true;
        }

        protected string SafeEval(string propName)
        {
            return Server.HtmlEncode(Eval(propName).ToString());            
        }

        protected string GetLocationByID()
        {
            int id = (int)Eval("LocationID");
            return Server.HtmlEncode(GetOperations.GetLocationNameByID(id));
        }

        protected string GetCategoryByID()
        {
            int id = (int)Eval("CategoryID");
            return Server.HtmlEncode(GetOperations.GetCategoryNameByID(id));
        }

        protected void ButtonCreateEvent_Click(object sender, EventArgs e)
        {      
            if (DropDownListLocations.SelectedValue != null && DropDownListCategories.SelectedValue != null
                && TextBoxEventTitle.Text != "" && RadCalendarEventDate.SelectedDate>DateTime.MinValue)
            {
                decimal money;
                
                if (0>TextBoxEventTitle.Text.Length&&
                    TextBoxEventTitle.Text.Length>150)
                {
                    LabelInvalidTitle.Visible = true;                    
                }else if (!decimal.TryParse(TextBoxPrice.Text,out money))
	            {
                    LabelErrorCreate.Text = "Invalid Price!";
                    LabelErrorCreate.Visible = true;
	            }
                else
                {
                    TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
                    Event ev = new Event
                    {
                        CategoryID=int.Parse(DropDownListCategories.SelectedValue),
                        LocationID = int.Parse(DropDownListLocations.SelectedValue),
                        Title=TextBoxEventTitle.Text,
                        DateTime=RadCalendarEventDate.SelectedDate,
                        Price=money
                    };
                    dbContext.Events.AddObject(ev);
                    dbContext.SaveChanges();
                    dbContext.Dispose();
                    LabelErrorCreate.Text = "Create Successful!";
                    LabelErrorCreate.Visible = true;
                    ListViewEvents.DataBind();
                }
            }
            else
            {
                LabelErrorCreate.Visible = true;
            }
        }

        protected void ButtonEdit_Command(object sender, CommandEventArgs e)
        {
            Server.Transfer("~/Admin/EditEvent.aspx?id=" + e.CommandArgument);
        }

        protected void ButtonDelete_Command(object sender, CommandEventArgs e)
        {
            if (GetOperations.DeleteEventByID(int.Parse(e.CommandArgument.ToString())))
            {
                Response.Redirect("~/Admin/Events.aspx");
                LabelError.Visible = false;
            }
            else
            {
                LabelError.Visible = true;
            }  
        }
    }
}